#!/bin/bash
go env
GOROOT_230="${GOROOT}230"
GOROOT_COPY="${GOROOT_230}copy"

echo $GOROOT_230
echo $GOROOT_COPY

# copy go root to another directory
cp -rf -v $GOROOT $GOROOT_230
cp -rf -v $GOROOT $GOROOT_COPY

# remove old version
cd $GOROOT_230/src

rm -rf -v $GOROOT_230/src/chainmaker/
rm -rf -v $GOROOT_COPY/src/chainmaker/
rm -rf -v $GOROOT_230/pkg/linux_amd64/chainmaker/
rm -rf -v $GOROOT_COPY/pkg/linux_amd64/chainmaker/

## clone mock sdk
echo $GOROOT_230/src
cd $GOROOT_230/src
git clone -b v2.3.0_contract_demo git@git.code.tencent.com:ChainMaker/contract-sdk-docker-go-ide.git
mv contract-sdk-docker-go-ide chainmaker

go install github.com/visualfc/gotools@latest
go install github.com/nsf/gocode@latest

# remove no use file
rm -rf -v $GOROOT_230/src/chainmaker/contract*
rm -rf -v $GOROOT_230/src/chainmaker/.DS_Store
rm -rf -v $GOROOT_230/src/chainmaker/test
rm -rf -v $GOROOT_230/src/chainmaker/.git*
rm -rf -v $GOROOT_230/src/chainmaker/go.mod
rm -rf -v $GOROOT_230/src/chainmaker/go.sum
rm -rf -v $GOROOT_230/src/chainmaker/pb/proto/
rm -rf -v $GOROOT_230/src/chainmaker/pb/Makefile

# replace chainmaker-contract-sdk-docker-go to chainmaker
#sed -i "s/chainmaker.org\\/chainmaker-contract-sdk-docker-go/chainmaker/g" $(grep chainmaker-contract-sdk-docker-go -rl $GOROOT_230/src/chainmaker)

## clone actual sdk
#echo $GOROOT_COPY/src
#cd $GOROOT_COPY/src
#git clone -b v2.2.0_alpha_qc https://git.code.tencent.com/ChainMaker/chainmaker-contract-sdk-docker-go chainmaker
#
## remove no use file
#rm -rf -v $GOROOT_COPY/src/chainmaker/.git/
#rm -rf -v $GOROOT_COPY/src/chainmaker/demo/
#rm -v $GOROOT_COPY/src/chainmaker/go.mod $GOROOT_COPY/src/chainmaker/go.sum $GOROOT_COPY/src/chainmaker/README.md
#rm -v $GOROOT_COPY/src/chainmaker/main.go $GOROOT_COPY/src/chainmaker/Makefile $GOROOT_COPY/src/chainmaker/build.sh
#rm -v $GOROOT_COPY/src/chainmaker/.gitignore
#rm -rf -v $GOROOT_COPY/src/chainmaker/pb/proto/
#
## replace chainmaker-contract-sdk-docker-go to chainmaker
#sed -i "s/chainmaker.org\\/chainmaker\\/chainmaker-contract-sdk-docker-go/chainmaker/g" $(grep chainmaker-contract-sdk-docker-go -rl $GOROOT_COPY/src/chainmaker)

# install new module
export GOROOT=$GOROOT_230
cd $GOROOT_230/src
go env -w GO111MODULE="on"
go env -w GOPROXY=https://goproxy.cn,direct
go mod tidy
go mod vendor
go env -w GO111MODULE="off"
cd $GOROOT_230/src
go install -i -a ./chainmaker/...
go env -w GO111MODULE="on"

cd $GOROOT_COPY/src
GOROOT_230=$GOROOT_COPY go env -w GO111MODULE="on"
GOROOT_230=$GOROOT_COPY go env -w GOPROXY=https://goproxy.cn,direct
GOROOT_230=$GOROOT_COPY go mod tidy
GOROOT_230=$GOROOT_COPY go mod vendor
GOROOT_230=$GOROOT_COPY go env -w GO111MODULE="off"
GOROOT_230=$GOROOT_COPY cd $GOROOT_COPY/src
GOROOT_230=$GOROOT_COPY go install -i -a ./chainmaker/...
GOROOT_230=$GOROOT_COPY go env -w GO111MODULE="on"

exit
